<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: gv_mail.php 53 2013-09-21 11:57:39Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 53 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:57:39 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

require(DIR_WS_CLASSES.'currencies.php');
$currencies = new currencies();

require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.phpmailer.php');
require_once(DIR_FS_INC.'inc.php_mail.php');
 

  if ( ($_GET['action'] == 'send_email_to_user') && ($_POST['customers_email_address'] || $_POST['email_to']) && (!$_POST['back_x']) ) {
    switch ($_POST['customers_email_address']) {
      case '***':
        $mail = $db->db_query("SELECT customers_firstname, customers_lastname, customers_email_address from ".TABLE_CUSTOMERS);
        $mail_sent_to = TEXT_ALL_CUSTOMERS;
        break;
      case '**D':
        $mail = $db->db_query("SELECT customers_firstname, customers_lastname, customers_email_address from ".TABLE_CUSTOMERS." WHERE customers_newsletter = '1'");
        $mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
        break;
      default:
        $customers_email_address =$_POST['customers_email_address'];

        $mail = $db->db_query("SELECT customers_firstname, customers_lastname, customers_email_address from ".TABLE_CUSTOMERS." WHERE customers_email_address = '".$customers_email_address."'");
        $mail_sent_to = $_POST['customers_email_address'];
        if ($_POST['email_to']) {
          $mail_sent_to = $_POST['email_to'];
        }
        break;
    }
	$smarty = new Smarty;
	
    $from =$_POST['from'];
    $subject =$_POST['subject'];

    while (!$mail->EOF) {
		$id1 = create_coupon_code($mail->fields['customers_email_address']);

		$smarty->assign('language', $_SESSION['language']);
		$smarty->caching = false;

		$smarty->assign('logo_path', HTTP_SERVER.DIR_WS_CATALOG.DIR_WS_IMAGES);

		$smarty->assign('AMMOUNT', $currencies->format($_POST['amount']));
		$smarty->assign('MESSAGE', $_POST['message']);
		$smarty->assign('GIFT_ID', $id1);
		$smarty->assign('WEBSITE', HTTP_SERVER.DIR_WS_CATALOG);

		$link = HTTP_SERVER.DIR_WS_CATALOG.'gv_redeem.php'.'?gv_no='.$id1;

		$smarty->assign('GIFT_LINK',$link);
		
		require_once (DIR_FS_INC.'inc.get_mail_body.php');
		$html_mail = $smarty->fetch('html:send_gift');
		$html_mail .= $signatur_html;
		$txt_mail = $smarty->fetch('txt:send_gift');
		$txt_mail .= $signatur_text;
		require_once (DIR_FS_INC.'inc.get_mail_data.php');
		$mail_data = get_mail_data('send_gift');
		
		$gv_mail_name = str_replace('{$shop}', STORE_NAME, $mail_data['EMAIL_ADDRESS_NAME']);
		
		if ($subject == '') 
			$subject = $mail_data['EMAIL_SUBJECT'];
		
      	php_mail($mail_data['EMAIL_ADDRESS'],
					$gv_mail_name, 
					$mail->fields['customers_email_address'] , 
					$mail->fields['customers_firstname'].' '.$mail->fields['customers_lastname'] ,
					'', 
					$mail_data['EMAIL_REPLAY_ADDRESS'], 
					$mail_data['EMAIL_REPLAY_ADDRESS_NAME'], 
					'', 
					'', 
					$subject, 
					$html_mail , 
					$txt_mail);

	  // Now create the coupon main and email entry
      $insert = $db->db_query("INSERT INTO ".TABLE_COUPONS." (coupon_code, coupon_type, coupon_amount, date_created) VALUES ('".$id1."', 'G', '".$_POST['amount']."', NOW())");
      $insert_id = $db->db_insert_id($insert);
      $insert = $db->db_query("INSERT INTO ".TABLE_COUPON_EMAIL_TRACK." (coupon_id, customer_id_sent, sent_firstname, emailed_to, date_sent) VALUES ('".$insert_id ."', '0', 'Admin', '".$mail->fields['customers_email_address']."', now() )");
    	$mail->MoveNext();
	}
	if ($_POST['email_to']) {
		$id1 = create_coupon_code($_POST['email_to']);

		$smarty->assign('AMMOUNT', $currencies->format($_POST['amount']));
		$smarty->assign('MESSAGE', stripslashes($_POST['message']));
		$smarty->assign('GIFT_ID', $id1);
		$smarty->assign('WEBSITE', HTTP_SERVER.DIR_WS_CATALOG);

		$link = HTTP_SERVER.DIR_WS_CATALOG.'gv_redeem.php'.'?gv_no='.$id1;

		$smarty->assign('GIFT_LINK',$link);

		require_once (DIR_FS_INC.'inc.get_mail_body.php');
		$html_mail = $smarty->fetch('html:send_gift');
		$html_mail .= $signatur_html;
		$txt_mail = $smarty->fetch('txt:send_gift');
		$txt_mail .= $signatur_text;
		require_once (DIR_FS_INC.'inc.get_mail_data.php');
		$mail_data = get_mail_data('send_gift');

		$gv_mail_name = str_replace('{$shop}', STORE_NAME, $mail_data['EMAIL_ADDRESS_NAME']);

		php_mail($mail_data['EMAIL_ADDRESS'],
					$gv_mail_name, 
					$_POST['email_to'] , 
					'' , 
					'', 
					$mail_data['EMAIL_REPLAY_ADDRESS'], 
					$mail_data['EMAIL_REPLAY_ADDRESS_NAME'], 
					'', 
					'', 
					$mail_data['EMAIL_SUBJECT'], 
					$html_mail , 
					$txt_mail);

      $insert = $db->db_query("INSERT INTO ".TABLE_COUPONS." (coupon_code, coupon_type, coupon_amount, date_created) VALUES ('".$id1."', 'G', '".$_POST['amount']."', NOW())");
      $insert_id = $db->db_insert_id($insert);
      $insert = $db->db_query("INSERT INTO ".TABLE_COUPON_EMAIL_TRACK." (coupon_id, customer_id_sent, sent_firstname, emailed_to, date_sent) VALUES ('".$insert_id ."', '0', 'Admin', '".$_POST['email_to']."', NOW() )");
    }
    redirect(href_link(FILENAME_GV_MAIL, 'mail_sent_to='.urlencode($mail_sent_to)));
  }

if (($_GET['action'] == 'preview') && (!$_POST['customers_email_address']) && (!$_POST['email_to']))
	$message_stack->add(ERROR_NO_CUSTOMER_SELECTED, 'error');

if (($_GET['action'] == 'preview') && (!$_POST['amount']))
	$message_stack->add(ERROR_NO_AMOUNT_SELECTED, 'error');

if ($_GET['mail_sent_to'])
	$message_stack->add(sprintf(NOTICE_EMAIL_SENT_TO, $_GET['mail_sent_to']), 'success');

require(DIR_WS_INCLUDES.'metatag.php'); ?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
	<table class="outerTable">
  		<tr>
    		<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
				<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
			</td>
    		<td class="boxCenter" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      			<tr>
        			<td width="100%">
						<table border="0" width="100%" cellspacing="0" cellpadding="0">
          					<tr>
            					<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
          					</tr>
        				</table>
					</td>
      			</tr>
      			<tr>
        			<td>
						<table border="0" width="100%" cellspacing="0" cellpadding="2">
						<?php
						  if ( ($_GET['action'] == 'preview') && ($_POST['customers_email_address'] || $_POST['email_to']) ) {
						    switch ($_POST['customers_email_address']) {
						      case '***':
						        $mail_sent_to = TEXT_ALL_CUSTOMERS;
						        break;
						      case '**D':
						        $mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
						        break;
						      default:
						        $mail_sent_to = $_POST['customers_email_address'];
						        if ($_POST['email_to']) {
						          $mail_sent_to = $_POST['email_to'];
						        }
						        break;
						    }
						?>
          <tr><?php echo draw_form('mail', FILENAME_GV_MAIL, 'action=send_email_to_user'); ?>
            <td><table border="0" width="100%" cellpadding="0" cellspacing="2">
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td class="main"><b><?php echo TEXT_CUSTOMER; ?></b><br /><?php echo $mail_sent_to; ?></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_FROM; ?></b><br /><?php echo htmlspecialchars(stripslashes($_POST['from'])); ?></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_SUBJECT; ?></b><br /><?php echo htmlspecialchars(stripslashes($_POST['subject'])); ?></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_AMOUNT; ?></b><br /><?php echo nl2br(htmlspecialchars(stripslashes($_POST['amount']))); ?></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_MESSAGE; ?></b><br /><?php echo $_POST['message']; ?></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td>
				<?php
				/* Re-Post all POST'ed variables */
				    reset($_POST);
				    while (list($key, $value) = each($_POST)) {
				      if (!is_array($_POST[$key])) {
				        echo draw_hidden_field($key, htmlspecialchars(stripslashes($value))).'<br />';
				      }
				    }
				?>
                <table border="0" width="100%" cellpadding="0" cellspacing="2">
                  <tr>
                    <td><?php echo '<input type="submit" class="button" name="back" onClick="this.blur();" value="'.BUTTON_BACK.'"/>'; ?></td>
                    <td align="right"><?php echo '<a class="button" onClick="this.blur();" href="'.href_link(FILENAME_GV_MAIL).'">'.BUTTON_CANCEL.'</a> <input type="submit" class="button" onClick="this.blur();" value="'.BUTTON_SEND_EMAIL.'"/>'; ?></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
          <?php echo '</form>'; ?></tr>
			<?php
			  } else {
			?>
          <tr><?php echo draw_form('mail', FILENAME_GV_MAIL, 'action=preview'); ?>
            <td><table border="0" cellpadding="0" cellspacing="2">
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
				<?php
				    if ($_GET['cID']) {
				    $select='where customers_id='.$_GET['cID'];
				    } else {
				    $customers = array();
				    $customers[] = array('id' => '', 'text' => TEXT_SELECT_CUSTOMER);
				    $customers[] = array('id' => '***', 'text' => TEXT_ALL_CUSTOMERS);
				    $customers[] = array('id' => '**D', 'text' => TEXT_NEWSLETTER_CUSTOMERS);
				    }
				    $customers_values = $db->db_query("SELECT customers_id, customers_email_address, customers_firstname, customers_lastname from ".TABLE_CUSTOMERS." ".$select." ORDER BY customers_lastname");
				    while(!$customers_values->EOF) {
				      $customers[] = array('id' => $customers_values->fields['customers_email_address'],
				                           'text' => $customers_values->fields['customers_lastname'].', '.$customers_values->fields['customers_firstname'].' ('.$customers_values->fields['customers_email_address'].')');
				    $customers_values->MoveNext();
					}
				?>
              <tr>
                <td class="main"><?php echo TEXT_CUSTOMER; ?></td>
                <td class="main"><?php echo draw_pull_down_menu('customers_email_address', $customers, $_GET['customer']);?></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
               <tr>
                <td class="main"><?php echo TEXT_TO; ?></td>
                <td class="main"><?php echo draw_input_field('email_to'); ?><?php echo '&nbsp;&nbsp;'.TEXT_SINGLE_EMAIL; ?></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
             <tr>
                <td class="main"><?php echo TEXT_FROM; ?></td>
                <td class="main"><?php echo draw_input_field('from', EMAIL_FROM); ?></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td class="main"><?php echo TEXT_SUBJECT; ?></td>
                <td class="main"><?php echo draw_input_field('subject'); ?></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td valign="top" class="main"><?php echo TEXT_AMOUNT; ?></td>
                <td class="main"><?php echo draw_input_field('amount'); ?></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td valign="top"><?php echo TEXT_MESSAGE; ?></td>
                <td><?php echo wysiwyg('message'); ?></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2" align="right"><?php echo '<input type="submit" class="button" onclick="this.blur();" value="'.BUTTON_SEND_EMAIL.'"/>'; ?></td>
              </tr>
            </table></td>
          <?php echo '</form>'; ?></tr>
		<?php
		  }
		?>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>